#include "SubCmd.h"
#include <sstream>
#include <iostream>
#include "CmdEnvironment.h"
#include "ObjController.h"
using namespace std;

CSubCmd::CSubCmd(void)
{
}


CSubCmd::~CSubCmd(void)
{
}

bool CSubCmd::ExcuteImpl(COMMAND& command)
{
	WnString& cmd = command.command;
	stringstream ss;
	if(CMD_HELP == cmd)
	{
		return ShowHelpInfo(command);
	}
	else if(CMD_INFO == cmd)
		command.group = CMD_INFO;
	else if(CMD_RUN == cmd)
		command.group = CMD_RUN;
	else
		return false;

	return true;
}

bool CSubCmd::ShowHelpInfo(COMMAND& command)
{
	bool ret = false;
	OBJ_MAP& allObjects = CObjController::Instance().Get();
	for(OBJ_MAP::iterator it = allObjects.begin(); it != allObjects.end(); ++it)
	{
		CWindObj* ptrObj = (*it).second;
		WnString object_name = ptrObj->GetName();
		WnString object_alias = ptrObj->GetAlias();

		MAP_STR_MAP_STR_STR& commandTags = ptrObj->GetCommandTag();
		if(commandTags.find(command.group) != commandTags.end())
		{
			stringstream ss;
			ss << object_name << " | " << object_alias << "\n";
			m_cmdResult += ss.str();

			MAP_STR_STR& subCommands = commandTags[command.group];
			for(MAP_STR_STR::iterator it2 = subCommands.begin(); it2 != subCommands.end(); ++it2)
			{
				ss.str("");
				ss << "\t" << (*it2).first << "\t" << (*it2).second;
				ss << endl;
				m_cmdResult += ss.str();
			}
			ret = true;
		}
	}

	return ret;
}